package com.elitel.frame.base.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elitel.frame.base.component.RedisCacheGroupServer;
import com.elitel.frame.base.service.GroupService;
import com.elitel.frame.main.dao.BaseServicesMapper;
import com.elitel.frame.main.dao.CfgGroupMapper;
import com.elitel.frame.main.dao.ext.ExtGroupMapper;
import com.elitel.frame.main.entity.BaseServices;
import com.elitel.frame.main.entity.CfgGroup;
import com.elitel.frame.main.entity.ext.CfgGroupExt;
import com.elitel.frame.main.entity.vo.KeyValueModelVo;
import com.elitel.frame.main.entity.vo.ZTreeDataModelVo;

/**
 * @Description 分组维护服务实现类
 * @author gyf
 */
@Service
public class GroupServiceImpl implements GroupService {

	@Autowired
	private ExtGroupMapper extGroupMapper;
	@Autowired
	private  CfgGroupMapper cfgGroupMapper;
	@Autowired
	private BaseServicesMapper baseServicesMapper;
	@Autowired
	private RedisCacheGroupServer redisCacheGroupServer;
	

	@Override
	public List<CfgGroupExt> getAllList(Integer fatureid) {
		List<CfgGroupExt> returnList = new ArrayList<CfgGroupExt>();
		 List<CfgGroupExt> parentList = extGroupMapper.getAllList(fatureid);
		 if(parentList.size() > 0){
			 for (CfgGroupExt cfgGroup : parentList) {
				returnList.add(cfgGroup);
				returnList.addAll(getAllList(cfgGroup.getGroupid()));
			}
		 }
		 return returnList;
	}

	@Override
	public int addCfgGroup(CfgGroup cfgGroup) { 
		int insertSelective = cfgGroupMapper.insertSelective(cfgGroup);
		redisCacheGroupServer.freshGroupListForTree();
		return insertSelective;
	}

	@Override
	public int updateCfgGroup(CfgGroup cfgGroup) {
		int updateByPrimaryKeySelective = cfgGroupMapper.updateByPrimaryKeySelective(cfgGroup);
		redisCacheGroupServer.freshGroupListForTree();
		return updateByPrimaryKeySelective;
	}

	@Override
	public int delCfgGroup(Integer groupId) {
		int deleteByPrimaryKey = cfgGroupMapper.deleteByPrimaryKey(groupId);
		redisCacheGroupServer.freshGroupListForTree();
		return deleteByPrimaryKey;
	}

	@Override
	public List<KeyValueModelVo> getServiceList(String serviceId) {
		BaseServices baseServices = baseServicesMapper.selectByPrimaryKey(serviceId);
		if(baseServices !=null){
			String tablename = baseServices.getLinktable();
			String paramtype = null;
			if(baseServices.getServicetype() !=null && baseServices.getServicetype().equals("") == false){
				paramtype =baseServices.getServicetype().toString();
			}
			Map<String, String> map= new HashMap<String, String>();
			map.put("strtableName", tablename);
			map.put("paramtype",paramtype);
			return extGroupMapper.getServiceInfo(map);
		}else{
			return null;
		}
	}

	@Override
	public List<ZTreeDataModelVo> getGroupListForTree(Integer fatureid) {
		List<ZTreeDataModelVo> parentList = extGroupMapper.getGroupList(fatureid);
		if(parentList.size() > 0){
			for (ZTreeDataModelVo zTreeDataModelVo : parentList) {
				zTreeDataModelVo.setIconClose("css/zTree/zTreeStyle/img/diy/1_open.png");
				zTreeDataModelVo.setIconOpen("css/zTree/zTreeStyle/img/diy/1_close.png");
				zTreeDataModelVo.setIcon("css/zTree/zTreeStyle/img/diy/8.png");
				zTreeDataModelVo.setChildren(getGroupListForTree(Integer.valueOf( zTreeDataModelVo.getId())));
			}
		}
		return parentList;
	}

	@Override
	public CfgGroup getGroupBySystemId(String systemId) {
		CfgGroup selectByPrimaryKey = cfgGroupMapper.selectByPrimaryKey(Integer.valueOf(systemId));
		return selectByPrimaryKey;
	}
	
	
}
